package com.haier.cre.commonframe.base;

import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.Vibrator;
import android.view.Window;

import com.haier.cre.commonframe.swipeback.SwipeBackLayout;
import com.haier.cre.commonframe.swipeback.app.SwipeBackActivity;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;

/**
 * 项目名：CommonLibrary
 * 包 名：com.haier.cre.commonframe.base
 * 创建人：18002063
 * 日 期：2018/10/16
 * 简 述：包含侧滑关闭Activity,EventBus的注册反注册,关闭应用等功能
 */
public class LibActivity extends SwipeBackActivity {
    /**
     * tag,用于打印log,如L.i(TAG,"xxxxxx")
     */
    protected final String TAG = this.getClass().getSimpleName();

    private static final int VIBRATE_DURATION = 20;

    /**
     * 由于会内存泄漏,暂时未使用
     */
    private static List<Activity> mActivities = new LinkedList<Activity>();

    /**
     * 侧滑返回
     */
    public SwipeBackLayout mSwipeBackLayout;

    /**
     * 判断Activity是否关闭
     * 主要用于Dialog打开之前的判断,如果Activity关闭了,会发生闪退
     */
    protected boolean isActivityFinish = false;


    @Override
    @Subscribe
    public void onCreate(Bundle savedInstanceState) {
        // 竖屏锁定
        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        //去除title
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        isActivityFinish = false;

        synchronized (mActivities) {
            mActivities.add(this);
        }

        //注册EventBus
        EventBus.getDefault().register(this);
        initSwipeBack();
    }

    /**
     * 侧滑关闭Activity
     */
    private void initSwipeBack() {
        mSwipeBackLayout = getSwipeBackLayout();
        mSwipeBackLayout.setEdgeTrackingEnabled(SwipeBackLayout.EDGE_LEFT);
        mSwipeBackLayout.addSwipeListener(new SwipeBackLayout.SwipeListener() {
            @Override
            public void onScrollStateChange(int state, float scrollPercent) {
            }

            @Override
            public void onEdgeTouch(int edgeFlag) {
                vibrate(VIBRATE_DURATION);
            }

            @Override
            public void onScrollOverThreshold() {
                vibrate(VIBRATE_DURATION);
            }
        });
    }


    @Override
    protected void onDestroy() {
        //反注册EventBus
        EventBus.getDefault().unregister(this);
        super.onDestroy();
        synchronized (mActivities) {
            mActivities.remove(this);
        }
        isActivityFinish = true;
    }

    /**
     * 关闭所有的activity
     */
    public static void finishAll() {
        // 遍历所有的activity，finish
        ListIterator<Activity> iterator = mActivities.listIterator();
        while (iterator.hasNext()) {
            Activity next = iterator.next();
            next.finish();
        }
    }

    /**
     * 应用程序退出
     */
    public static void appExit() {
        try {
            //            finishAll();
            //退出JVM(java虚拟机),释放所占内存资源,0表示正常退出(非0的都为异常退出)
            System.exit(0);
            //从操作系统中结束掉当前程序的进程
            android.os.Process.killProcess(android.os.Process.myPid());
        } catch (Exception e) {
            System.exit(-1);
        }
    }

    private void vibrate(long duration) {
        Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
        long[] pattern = {
                0, duration
        };
        vibrator.vibrate(pattern, -1);
    }
}
